<!DOCTYPE html>
<html lang="zh-cn">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>电机控制</title>
    <style>
        body {
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            margin: 0;
            font-family: Arial, sans-serif;
            background-color: #f0f0f0;
        }
        .container {
            display: flex;
            flex-direction: column;
            align-items: center;
            height: 90%;
            justify-content: space-around;
        }
        input[type="range"] {
            width: 120%;
            height: 50px;
            margin: 20px 0;
            background-color: #007bff;
        }
        .controls {
            display: flex;
            flex-direction: column;
            align-items: center;
            width: 100%;
        }
        .controls button {
            padding: 10px 20px;
            font-size: 18px;
            color: #fff;
            background-color: #007bff;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
            width: 150px;
            text-align: center;
            margin: 10px 0;
        }
        .controls button:active {
            background-color: #0056b3;
        }
    </style>
    <script>
        var motorRunning = false;

        function toggleMotor(action) {
            var xhr = new XMLHttpRequest();
            xhr.open('GET', '/' + action, true);
            xhr.send();
            motorRunning = (action === 'start');
            if (motorRunning) {
                setSpeed();
            }
        }

        function setSpeed() {
            var speed = document.getElementById("speedSlider").value;
            document.getElementById("speedValue").innerText = speed;
            var xhr = new XMLHttpRequest();
            xhr.open('GET', '/setSpeed?value=' + speed, true);
            xhr.send();
        }

        function toggleLight() {
            var xhr = new XMLHttpRequest();
            xhr.open('GET', '/toggleLight', true);
            xhr.send();
        }
    </script>
</head>
<body>
    <div class="container">
        <div id="speedValue">0</div>
        <input type="range" id="speedSlider" min="-10000" max="10000" value="0" oninput="document.getElementById('speedValue').innerText = this.value;">
        <div class="controls">
            <button onclick="toggleMotor('start')">运行电机</button>
            <button onclick="toggleMotor('stop')">停止电机</button>
            <button onclick="toggleLight()">开关灯</button>
        </div>
    </div>
</body>
</html>
